package com.dunqi.gpm.cangxi.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioGroup;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.base.ComObserver;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.util.TimeUtil;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.adapter.SpinnerAdapter;
import com.dunqi.gpm.cangxi.home.bean.BelongType;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class ProjectInquiryActivity extends BaseAct {

    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.spinner1)
    AppCompatSpinner spinner1;
    @BindView(R.id.spinner2)
    AppCompatSpinner spinner2;
    @BindView(R.id.spinner3)
    AppCompatSpinner spinner3;
    @BindView(R.id.spinner4)
    AppCompatSpinner spinner4;
    @BindView(R.id.spinner5)
    AppCompatSpinner spinner5;
    @BindView(R.id.spinner6)
    AppCompatSpinner spinner6;
    @BindView(R.id.home_project_state_rg)
    RadioGroup homeProjectStateRg;
    @BindView(R.id.home_important_state_rg)
    RadioGroup homeImportantStateRg;

    ArrayList<BelongType> data = new ArrayList<>();

    ArrayList<BelongType> data2 = new ArrayList<>();
    ArrayList<BelongType> data3 = new ArrayList<>();
    ArrayList<BelongType> data4 = new ArrayList<>();
    ArrayList<BelongType> data5 = new ArrayList<>();
    ArrayList<BelongType> data6 = new ArrayList<>();

    private BelongType item;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

        net("project_invest_range,industry_category,project_important_type,project_source,project_batch_category");

    }

    private void net(String group) {
        ApiFactory.getInstance()
                .getListByGroup(group)
                .compose(RxSchedulers.<Data<ArrayList<BelongType>>>io_main())
                .subscribe(new ComObserver<ArrayList<BelongType>>() {
                    @Override
                    public void onSuccess(ArrayList<BelongType> belongTypes) {
                        if (belongTypes == null) {
                            return;
                        }
                        data.clear();

                        data.addAll(belongTypes);


                        refreshUi();

                    }

                    @Override
                    public void onFinal() {

                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }
                });
    }

    private void refreshUi() {
        init();

        for (BelongType datum : data) {

            switch (datum.getGroupCode()) {
                case "industry_category"://行业大类
                    if ("-1".equals(datum.getParentCategoryCode())) {
                        data2.add(datum);
                    }
                    break;
                case "project_source"://项目属性
                    data4.add(datum);
                    break;
                case "project_invest_range"://投资额度
                    data5.add(datum);
                    break;
                case "project_important_type"://重要类型
                    data6.add(datum);
                    break;
                default:
            }
        }
        spinner2.setAdapter(new SpinnerAdapter(getActivity(), convert(data2)));
        spinner4.setAdapter(new SpinnerAdapter(getActivity(), convert(data4)));
        spinner5.setAdapter(new SpinnerAdapter(getActivity(), convert(data5)));
        spinner6.setAdapter(new SpinnerAdapter(getActivity(), convert(data6)));

    }


    private void init() {
        data2.clear();
        data2.add(item);
        data3.clear();
        data3.add(item);
        data4.clear();
        data4.add(item);
        data5.clear();
        data5.add(item);
        data6.clear();
        data6.add(item);
    }

    String[] years = TimeUtil.get11Year();

    @Override
    protected void initUiAndListener() {
        item = new BelongType();
        item.setCategoryName("--全部--");

        spinner1.setAdapter(new SpinnerAdapter(this, years));

        init();

        data3.add(item);

        spinner2.setAdapter(new SpinnerAdapter(getActivity(), convert(data2)));
        spinner3.setAdapter(new SpinnerAdapter(getActivity(), convert(data3)));
        spinner4.setAdapter(new SpinnerAdapter(getActivity(), convert(data4)));
        spinner5.setAdapter(new SpinnerAdapter(getActivity(), convert(data5)));
        spinner6.setAdapter(new SpinnerAdapter(getActivity(), convert(data6)));


        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1) {
                    data3.clear();
                    data3.add(item);
                    spinner3.setAdapter(new SpinnerAdapter(getActivity(), convert(data3)));
                    return;
                }


                BelongType belongType = data2.get(position);
                String categoryCode = belongType.getCategoryCode();
                for (BelongType datum : data) {
                    if (categoryCode.equals(datum.getParentCategoryCode())) {
                        data3.add(datum);
                    }
                }
                spinner3.setAdapter(new SpinnerAdapter(getActivity(), convert(data3)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }

    private String[] convert(ArrayList<BelongType> list) {
        String[] strings = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            strings[i] = list.get(i).getCategoryName();
        }
        return strings;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_inquiry;
    }

    @OnClick(R.id.config_btn)
    public void onViewClicked() {
        commit();
    }

    HashMap<String, String> params = new HashMap<>();

    private void commit() {
        if (data.isEmpty()) {
            return;
        }

        params.clear();

        int yearPosition = spinner1.getSelectedItemPosition();
        if (yearPosition != 0) {
            String year = years[yearPosition];
            params.put("plan_s_year", year);
        }

        int industriesPosition = spinner2.getSelectedItemPosition();
        if (industriesPosition != 0) {
            params.put("fst_ind", data2.get(industriesPosition).getCategoryCode());
        }

        int smallIndustriesPosition = spinner3.getSelectedItemPosition();
        if (smallIndustriesPosition != 0) {
            params.put("sec_ind", data3.get(smallIndustriesPosition).getCategoryCode());
        }

        int projectBatchsPosition = spinner4.getSelectedItemPosition();
        if (projectBatchsPosition != 0) {
            params.put("pro_src", data4.get(projectBatchsPosition).getCategoryCode());
        }

        int investPosition = spinner5.getSelectedItemPosition();
        if (investPosition != 0) {
            params.put("invest_range", data5.get(investPosition).getCategoryCode());
        }

        int ipType6 = spinner6.getSelectedItemPosition();

        if (ipType6 != 0) {
            params.put("ip_type", data6.get(ipType6).getCategoryCode());
        }

        params.put("uda", "false");
        params.put("audit_status", "1,2,3,4,5");

//        ProjectListActivity.start(this, params);

        Intent intent = new Intent();
        intent.putExtra("list_photo",params);
        setResult(400,intent);
        finish();
    }
}
